package de.schnitzeljaeger.controller;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import de.schnitzeljaeger.R;
import de.schnitzeljaeger.model.dao.IGenericDao;
import de.schnitzeljaeger.model.dao.impl.DaoFactory;
import de.schnitzeljaeger.model.entity.IPaperChaseListData;
import de.schnitzeljaeger.model.entity.IPublishable;
import de.schnitzeljaeger.model.entity.PaperChase;

public class PaperChaseList extends Activity {

	static private final int CREATE	= Menu.FIRST;
	static private final int DELETE = Menu.FIRST + 1;
	static private final int PUBLISH = Menu.FIRST + 2;
	
	
	private class RowListener implements View.OnClickListener, View.OnLongClickListener {
		
		private long id;
		private String name;
		
		public RowListener(IPaperChaseListData paperChase) {
			this.id = paperChase.getId(); // to free paperChase for garbage collection
			this.name = paperChase.getName();
		}
		
		@Override
		public void onClick(View v) {
			update(id);
		}
		
		@Override
		public boolean onLongClick(View v) {
			selectedId = id;
			selectedName = name;
			v.showContextMenu();
			return true;
		}
	}
	
	private IGenericDao<PaperChase> dao;
	private TableLayout table;
	private Long selectedId;
	private String selectedName;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paper_chase_table);
        setTitle(R.string.schnitzeljagd_management);
        dao = DaoFactory.create(PaperChase.class, getApplicationContext());
        table = (TableLayout) findViewById(R.id.table);
        
        fillData();
    }
    
    /** Called if child activity is finished */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, CREATE, 0, R.string.create);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case CREATE:
                create();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle(selectedName);
        menu.add(0, DELETE, 0, R.string.delete);
        menu.add(0, PUBLISH, 1, R.string.publish_action);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case PUBLISH:
        	publishUnpublish(selectedId);
        	return true;
        case DELETE:
        	new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(selectedName)
            .setMessage(R.string.delete_paper_chase)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                	delete(selectedId);                	
                }
            })
            .setNegativeButton(R.string.no, null)
            .show();
        	return true;
        }
        
        return super.onContextItemSelected(item);
    }
    
    private void create() {
    	Intent i = new Intent(this, PaperChaseEdit.class);
        startActivityForResult(i, PaperChaseEdit.CREATE);
    }
    
    private void update(long id) {
    	Intent i = new Intent(this, PaperChaseEdit.class);
        i.putExtra(PaperChaseEdit.ID, id);
        startActivityForResult(i, PaperChaseEdit.UPDATE);
    }
    
    private void delete(long id) {
    	dao.delete(id);
        fillData();
    }
    
    private void publishUnpublish(long id) {
    	IPublishable p = dao.find(IPublishable.class, id);
    	
    	p.setPublished(!p.isPublished());
    	dao.update(IPublishable.class, p);
    	fillData();
    }
    
    private void fillData() {
    	table.removeAllViews();

    	List<IPaperChaseListData> paperChases = dao.list(IPaperChaseListData.class);
    	
    	for (IPaperChaseListData paperChase : paperChases)
    		addRow(paperChase);
    }
    
	private void addRow(IPaperChaseListData paperChase) {
    	TableRow tr = new TableRow(this);
    	RowListener rowListener = new RowListener(paperChase);
    	
    	registerForContextMenu(tr);

    	tr.setOnLongClickListener(rowListener);
    	tr.setOnClickListener(rowListener);

    	TextView lblName = new TextView(this);//(TextView) tr.findViewById(R.id.name);?
    	TextView lblDescription = new TextView(this);//(TextView) tr.findViewById(R.id.description);
    	TextView lblPublished = new TextView(this);

    	lblName.setText(paperChase.getName() + " (" + paperChase.getId() + ")");
    	lblDescription.setText(paperChase.getDescription());
    	
    	if (paperChase.isPublished()) {
    		lblPublished.setTextColor(Color.GREEN);
    		lblPublished.setText(R.string.published);
    	} else {
    		lblPublished.setTextColor(Color.RED);
    		lblPublished.setText(R.string.unpublished);
    	}

    	lblName.setWidth(100);
    	lblDescription.setWidth(150);
    	tr.addView(lblName);
    	tr.addView(lblDescription);
    	tr.addView(lblPublished);

    	table.addView(tr);
    }
}
